using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using BLL;
using DTO;

namespace WellmeadowsHospital
{
    public partial class frmAddPatients : DevExpress.XtraEditors.XtraUserControl
    {
        NextOfKinBLL nokBLL = new NextOfKinBLL();
        LocalDoctorBLL localdoctorBLL = new LocalDoctorBLL();
        PatientBLL patientBLL = new PatientBLL();
        AppointmentBLL appointmentBLL = new AppointmentBLL();
        //khai bao cac bien de edit trong tab add patient
        string[] pa_nok_ld_ID = new string[3];
        //bien de edit Appointment trong Add Appointment
        string appointmentid;

        public frmAddPatients()
        {
            InitializeComponent();
        }

        private void addPatientH_Click(object sender, EventArgs e)
        {
            try
            {
                NextOfKinDTO nhannok = new NextOfKinDTO();
                nhannok.Fullname = nextkinFullNameH.Text;
                nhannok.Relationship = nextkinRelationH.Text;
                nhannok.Address = nextkinAddressH.Text;
                nhannok.Tel = nextkinTelH.Text;
                LocalDoctorDTO nhanld = new LocalDoctorDTO();
                nhanld.Fullname = localdoctorFulNameH.Text;
                nhanld.Clinicno = localdoctorClinicNoH.Text;
                nhanld.Address = localdoctorAddressH.Text;
                nhanld.Tel = localdoctorTelH.Text;

                PatientDTO nhanpa = new PatientDTO();
                nhanpa.FirstName = paFirstNameH.Text;
                nhanpa.Lastname = paLastNameH.Text;
                nhanpa.Address = paAddressH.Text;
                nhanpa.Sex = paSexH.SelectedItem.ToString();
                nhanpa.Tel = paTelH.Text;
                nhanpa.Dob = paDobH.DateTime;
                nhanpa.Maritalstatus = paMariH.SelectedItem.ToString();
                nhanpa.Dateregistered = paDateRegisH.DateTime;
                nhanpa.Status = paStatusH.Text;
                string kt1 = nextkinFullNameH.Text;
                string kt2 = localdoctorFulNameH.Text;
                if (!kt1.Equals(""))//-----co nextofkin-----------------
                {
                    if (!kt2.Equals(""))//--------------co localdoctor------------------
                    {
                        if (nokBLL.Insert(nhannok).Equals("success") && localdoctorBLL.Insert(nhanld).Equals("success"))
                        {
                            nhanpa.Localdoctorid = localdoctorBLL.GetID(nhanld);
                            nhanpa.Nokid = nokBLL.GetID(nhannok);
                            if (patientBLL.Insert(nhanpa).Equals("success"))
                            {
                                MessageBox.Show("Insert patient, nextofkin and localdoctor successful");
                                gcPatients.DataSource = patientBLL.SelectDesc();
                            }
                            else
                            {
                                MessageBox.Show(patientBLL.Insert(nhanpa));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Insert into LocalDoctor, NextOfKin error");
                        }
                    }
                    else//-------co nextokkin, khong co localdoctor-----------
                    {
                        if (nokBLL.Insert(nhannok).Equals("success"))
                        {
                            nhanpa.Nokid = nokBLL.GetID(nhannok);
                            if (patientBLL.Insert(nhanpa).Equals("success"))
                            {
                                MessageBox.Show("Insert patient and nextofkin successful");
                                gcPatients.DataSource = patientBLL.SelectDesc();
                            }
                            else
                            {
                                MessageBox.Show(patientBLL.Insert(nhanpa));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Insert into NextOfKin error");
                        }
                    }
                }
                else//------------khong co nextofkin
                {
                    if (!kt2.Equals(""))//------co localdoctor------
                    {
                        if (localdoctorBLL.Insert(nhanld).Equals("success"))
                        {
                            nhanpa.Localdoctorid = localdoctorBLL.GetID(nhanld);
                            if (patientBLL.Insert(nhanpa).Equals("success"))
                            {
                                MessageBox.Show("Insert patient and localdoctor successful");
                                gcPatients.DataSource = patientBLL.SelectDesc();
                            }
                            else
                            {
                                MessageBox.Show(patientBLL.Insert(nhanpa));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Insert into LocalDoctor error");
                        }
                    }
                    else
                    {//-----------ko co nextofkin va localdoctoc
                        if (patientBLL.Insert(nhanpa).Equals("success"))
                        {
                            MessageBox.Show("Insert patient successful");
                            gcPatients.DataSource = patientBLL.SelectDesc();
                        }
                        else
                        {
                            MessageBox.Show(patientBLL.Insert(nhanpa));
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        private void buttonEditPatientH_Click(object sender, EventArgs e)
        {
            try
            {
                NextOfKinDTO nokpaDTO = new NextOfKinDTO();
                nokpaDTO.Nokid = pa_nok_ld_ID[1];
                nokpaDTO.Fullname = nextkinFullNameH.Text;
                nokpaDTO.Relationship = nextkinRelationH.Text;
                nokpaDTO.Address = nextkinAddressH.Text;
                nokpaDTO.Tel = nextkinTelH.Text;

                //LocalDoctorAndPatientDTO ldpaDTO = new LocalDoctorAndPatientDTO();
                LocalDoctorDTO ldpaDTO = new LocalDoctorDTO();
                ldpaDTO.Localdoctorid = pa_nok_ld_ID[2];
                ldpaDTO.Fullname = localdoctorFulNameH.Text;
                ldpaDTO.Clinicno = localdoctorClinicNoH.Text;
                ldpaDTO.Address = localdoctorAddressH.Text;
                ldpaDTO.Tel = localdoctorTelH.Text;

                PatientDTO nhanpa = new PatientDTO();
                nhanpa.Patientid = pa_nok_ld_ID[0];
                nhanpa.FirstName = paFirstNameH.Text;
                nhanpa.Lastname = paLastNameH.Text;
                nhanpa.Address = paAddressH.Text;
                nhanpa.Sex = paSexH.SelectedItem.ToString();
                nhanpa.Tel = paTelH.Text;
                nhanpa.Dob = paDobH.DateTime;
                nhanpa.Maritalstatus = paMariH.SelectedItem.ToString();
                nhanpa.Dateregistered = paDateRegisH.DateTime;
                nhanpa.Status = paStatusH.Text;
                string kt1 = nextkinFullNameH.Text;
                string kt2 = localdoctorFulNameH.Text;

                if (!kt1.Equals(""))//-----co nextofkin-----------------
                {
                    if (!kt2.Equals(""))//--------------co localdoctor------------------
                    {
                        if (nokBLL.Update(nokpaDTO).Equals("success") && localdoctorBLL.Update(ldpaDTO).Equals("success"))
                        {
                            nhanpa.Localdoctorid = ldpaDTO.Localdoctorid;
                            nhanpa.Nokid = nokpaDTO.Nokid;
                            if (patientBLL.Update(nhanpa).Equals("success"))
                            {
                                MessageBox.Show("Update patient, nextofkin and localdoctor successful");
                                gcPatients.DataSource = patientBLL.SelectDesc();
                            }
                            else
                            {
                                MessageBox.Show(patientBLL.Update(nhanpa));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Update into LocalDoctor, NextOfKin error");
                        }
                    }
                    else//-------co nextokkin, khong co localdoctor-----------
                    {
                        if (nokBLL.Update(nokpaDTO).Equals("success"))
                        {
                            nhanpa.Nokid = nokpaDTO.Nokid;
                            if (patientBLL.Update(nhanpa).Equals("success"))
                            {
                                MessageBox.Show("Update patient and nextofkin successful");
                                gcPatients.DataSource = patientBLL.SelectDesc();
                            }
                            else
                            {
                                MessageBox.Show(patientBLL.Update(nhanpa));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Update into NextOfKin error");
                        }
                    }
                }
                else//------------khong co nextofkin
                {
                    if (!kt2.Equals(""))//------co localdoctor------
                    {
                        if (localdoctorBLL.Update(ldpaDTO).Equals("success"))
                        {
                            nhanpa.Localdoctorid = ldpaDTO.Localdoctorid;
                            if (patientBLL.Update(nhanpa).Equals("success"))
                            {
                                MessageBox.Show("Update patient and localdoctor successful");
                                gcPatients.DataSource = patientBLL.SelectDesc();
                            }
                            else
                            {
                                MessageBox.Show(patientBLL.Update(nhanpa));
                            }
                        }
                        else
                        {
                            MessageBox.Show("Update into LocalDoctor error");
                        }
                    }
                    else
                    {//-----------ko co nextofkin va localdoctoc
                        if (patientBLL.Update(nhanpa).Equals("success"))
                        {
                            MessageBox.Show("Update patient successful");
                            gcPatients.DataSource = patientBLL.SelectDesc();
                        }
                        else
                        {
                            MessageBox.Show(patientBLL.Update(nhanpa));
                        }
                    }
                }

            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        private void paSearchH_KeyPress(object sender, KeyPressEventArgs e)
        {
            gcPatients.DataSource = patientBLL.Search(paSearchH.Text);
        }

        private void gridView16_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            buttonEditPatientH.Visible = true;
            string key = gridView16.GetRowCellValue(gridView16.GetSelectedRows()[0], gcPatientID).ToString();
            var pa = patientBLL.Search(key)[0];
            paFirstNameH.Text = pa.FirstName;
            paLastNameH.Text = pa.LastName;
            paAddressH.Text = pa.Address;
            paSexH.Text = pa.Sex;
            paTelH.Text = pa.Tel;
            //paDobH.DateTime = pa.DateOfBirth;
            paMariH.Text = pa.MaritalStatus;
            // paDateRegisH.DateTime = pa.DateRegistered;
            paStatusH.Text = pa.Status;
            pa_nok_ld_ID[0] = pa.PatientID;
            if (pa.NOKID != null)
            {
                var nok = nokBLL.SearchWithPatientID(key)[0];
                pa_nok_ld_ID[1] = nok.NOKID;
                nextkinFullNameH.Text = nok.FullName;
                nextkinRelationH.Text = nok.Relationship;
                nextkinTelH.Text = nok.Tel;
                nextkinAddressH.Text = nok.Address;
            }
            else
            {
                nextkinFullNameH.Text = "";
                nextkinRelationH.Text = "";
                nextkinTelH.Text = "";
                nextkinAddressH.Text = "";
            }
            if (pa.LocalDoctorID != null)
            {
                var ld = localdoctorBLL.SearchWithPatientID(key)[0];
                pa_nok_ld_ID[2] = ld.LocalDoctorID;
                localdoctorFulNameH.Text = ld.FullName;
                localdoctorClinicNoH.Text = ld.ClinicNo;
                localdoctorAddressH.Text = ld.Address;
                localdoctorTelH.Text = ld.Tel;
            }
            else
            {
                localdoctorFulNameH.Text = "";
                localdoctorClinicNoH.Text = "";
                localdoctorAddressH.Text = "";
                localdoctorTelH.Text = "";
            }
        }
    }
}
